package com.hzit.mmcb.core.controller.admin;


import com.hzit.common.exception.Assert;
import com.hzit.common.result.ResponseEnum;
import com.hzit.mmcb.core.pojo.entity.IntegralGrade;
import com.hzit.mmcb.core.pojo.vo.IntegralVO;
import com.hzit.mmcb.core.service.IntegralGradeService;
import com.hzit.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "会员积分等级管理")
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;

    @GetMapping("/list")
    @ApiOperation("查询积分等级列表")
    public R list(){
        List<IntegralGrade> list = integralGradeService.listAsc();
        return R.ok().data("list",list);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation("利用id删除积分等级")
    public R remove(@PathVariable @ApiParam(value = "id",required = true) int id){
        boolean b = integralGradeService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @GetMapping("/get/{id}")
    @ApiOperation("利用id查询积分等级")
    public R getById(@PathVariable @ApiParam(value = "id",required = true) int id){
        IntegralGrade byId = integralGradeService.getById(id);
        return R.ok().data("integralGrade",byId);
    }
    @PutMapping("/update")
    @ApiOperation("修改积分等级，需要带id")
    public R update(@RequestBody @ApiParam(value = "需要修改的积分等级",required = true ) IntegralVO integralVO){
        IntegralGrade integralGrade = new IntegralGrade();
        BeanUtils.copyProperties(integralVO,integralGrade);
        boolean exist=integralGradeService.isExist(integralGrade.getId(),integralGrade.getIntegralStart(),integralGrade.getIntegralEnd());
        Assert.isTrue(!exist, ResponseEnum.NOT_UPDATE_INTEGRAL_GRADE);
        boolean b = integralGradeService.updateById(integralGrade);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }

    @PostMapping("/add")
    @ApiOperation("增加积分等级，不能带id")
    public R add(@RequestBody @ApiParam(value = "需要添加的积分等级",required = true ) IntegralGrade integralGrade){
        boolean exist=integralGradeService.isExist(integralGrade.getIntegralStart(),integralGrade.getIntegralEnd());
        Assert.isTrue(!exist, ResponseEnum.NOT_ADD_INTEGRAL_GRADE);
        boolean b = integralGradeService.save(integralGrade);
        if (b){
            return R.ok().message("添加成功");
        }else {
            return R.error().message("添加失败");
        }
    }

}

